package com.fin.quant.rpc.bean;

import com.orbitz.consul.Consul;
import io.grpc.NameResolver;
import io.grpc.NameResolverProvider;
import io.grpc.NameResolverRegistry;

import java.net.URI;

public class ConsulNameResolverProvider extends NameResolverProvider {

    public Consul consul;

    public ConsulNameResolverProvider(Consul consul) {
        this.consul = consul;
    }


    @Override
    public NameResolver newNameResolver(URI targetUri, NameResolver.Args args) {
        if ("consul".equals(targetUri.getScheme())) {
            return new ConsulNameResolver(consul, targetUri.getHost());
        }
        return null;
    }

    @Override
    public String getDefaultScheme() {
        return "consul";
    }

    public static void register(Consul consul) {
        NameResolverRegistry.getDefaultRegistry().register(new ConsulNameResolverProvider(consul));
    }

    @Override
    protected boolean isAvailable() {
        return true;
    }

    @Override
    protected int priority() {
        return 5;
    }
}
